/**
 * @ProjectName:Backend
 * @Title: ShiroConfig
 * @Package com.phrp.config
 * @Description: shiro配置类
 * @author SuShuChao
 * @date 2024/4/7 20:46
 * @version V1.0
 * Copyright (c) 2024, 2034279227@qq.com All Rights Reserved.
 */
package com.phrp.config;

import com.phrp.shiro.AccountRealm;
import com.phrp.shiro.JwtFilter;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //ShiroFilter过滤所有请求
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给ShiroFilter配置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //配置系统受限资源
        //配置系统公共资源
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/managerUser/login","anon");//表示这个资源不需要认证授权
        map.put("/managerUser/logout","anon");//登出管理
        // map.put("/managerUser/tokenCheck","anon");// token检验
        map.put("/**","authc");//其余资源需要验证才可通过

        Map<String, Filter> filters = new LinkedHashMap<>();
        filters.put("authc", new JwtFilter());
        shiroFilterFactoryBean.setFilters(filters);

        //设置认证界面路径
        shiroFilterFactoryBean.setLoginUrl("http://localhost:3000/login");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }

    //创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }

    //创建自定义Realm
    @Bean
    public Realm getRealm(){
        AccountRealm realm = new AccountRealm();
        return realm;
    }
}
